package org.jeecg.modules.mes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.mes.entity.MesEmployeeWorkType;
import org.jeecg.modules.mes.service.IMesEmployeeWorkTypeService;
import org.jeecg.modules.mes.vo.MesEmployeeWorkTypeAssignVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工工种分配管理
 */
@Slf4j
@Api(tags = "员工工种分配管理")
@RestController
@RequestMapping("/mes/employee-worktype")
public class MesEmployeeWorkTypeController extends JeecgController<MesEmployeeWorkType, IMesEmployeeWorkTypeService> {

    @Autowired
    private IMesEmployeeWorkTypeService mesEmployeeWorkTypeService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "员工工种分配-分页列表查询")
    @ApiOperation(value = "员工工种分配-分页列表查询", notes = "员工工种分配-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MesEmployeeWorkType>> queryPageList(MesEmployeeWorkType mesEmployeeWorkType,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        // 创建分页对象
        Page<MesEmployeeWorkType> page = new Page<>(pageNo, pageSize);
        
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        
        // 添加查询条件
        params.put("employeeId", req.getParameter("employeeId"));
        params.put("employeeName", req.getParameter("employeeName_like"));
        params.put("workTypeId", req.getParameter("workTypeId"));
        params.put("workTypeName", req.getParameter("workTypeName_like"));
        params.put("skillLevel", req.getParameter("skillLevel"));
        params.put("status", req.getParameter("status"));
        
        // 调用Service查询
        IPage<MesEmployeeWorkType> pageList = mesEmployeeWorkTypeService.queryPageList(page, params);
        
        return Result.OK(pageList);
    }

    /**
     * 员工工种分配
     */
    @AutoLog(value = "员工工种分配-分配工种")
    @ApiOperation(value = "员工工种分配-分配工种", notes = "员工工种分配-分配工种")
    @PostMapping(value = "/assign-employee")
    public Result<?> assignWorkTypeToEmployee(@RequestBody MesEmployeeWorkTypeAssignVO assignVO) {
        return mesEmployeeWorkTypeService.assignWorkTypeToEmployee(assignVO);
    }

    /**
     * 根据员工ID获取工种列表
     */
    @AutoLog(value = "员工工种分配-根据员工ID获取工种列表")
    @ApiOperation(value = "员工工种分配-根据员工ID获取工种列表", notes = "员工工种分配-根据员工ID获取工种列表")
    @GetMapping(value = "/employee-worktypes/{employeeId}")
    public Result<List<MesEmployeeWorkType>> getEmployeeWorkTypes(@PathVariable String employeeId) {
        List<MesEmployeeWorkType> workTypes = mesEmployeeWorkTypeService.getEmployeeWorkTypes(employeeId);
        return Result.OK(workTypes);
    }

    /**
     * 添加
     */
    @AutoLog(value = "员工工种分配-添加")
    @ApiOperation(value = "员工工种分配-添加", notes = "员工工种分配-添加")
    @PostMapping(value = "/add")
    public Result<MesEmployeeWorkType> add(@RequestBody MesEmployeeWorkType mesEmployeeWorkType) {
        return mesEmployeeWorkTypeService.saveEmployeeWorkType(mesEmployeeWorkType);
    }

    /**
     * 编辑
     */
    @AutoLog(value = "员工工种分配-编辑")
    @ApiOperation(value = "员工工种分配-编辑", notes = "员工工种分配-编辑")
    @PutMapping(value = "/edit")
    public Result<MesEmployeeWorkType> edit(@RequestBody MesEmployeeWorkType mesEmployeeWorkType) {
        return mesEmployeeWorkTypeService.updateEmployeeWorkType(mesEmployeeWorkType);
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "员工工种分配-通过id删除")
    @ApiOperation(value = "员工工种分配-通过id删除", notes = "员工工种分配-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        return mesEmployeeWorkTypeService.removeEmployeeWorkType(id);
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "员工工种分配-批量删除")
    @ApiOperation(value = "员工工种分配-批量删除", notes = "员工工种分配-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return mesEmployeeWorkTypeService.removeEmployeeWorkTypes(idList);
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "员工工种分配-通过id查询")
    @ApiOperation(value = "员工工种分配-通过id查询", notes = "员工工种分配-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MesEmployeeWorkType> queryById(@RequestParam(name = "id", required = true) String id) {
        MesEmployeeWorkType mesEmployeeWorkType = mesEmployeeWorkTypeService.getById(id);
        if (mesEmployeeWorkType == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesEmployeeWorkType);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MesEmployeeWorkType mesEmployeeWorkType) {
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        
        // 添加查询条件
        params.put("employeeId", request.getParameter("employeeId"));
        params.put("employeeName", request.getParameter("employeeName_like"));
        params.put("workTypeId", request.getParameter("workTypeId"));
        params.put("workTypeName", request.getParameter("workTypeName_like"));
        params.put("skillLevel", request.getParameter("skillLevel"));
        params.put("status", request.getParameter("status"));
        
        // 调用Service查询所有数据
        Page<MesEmployeeWorkType> page = new Page<>(1, Integer.MAX_VALUE);
        IPage<MesEmployeeWorkType> pageList = mesEmployeeWorkTypeService.queryPageList(page, params);
        List<MesEmployeeWorkType> list = pageList.getRecords();

        return super.exportXlsByResult(request, MesEmployeeWorkType.class, "员工工种分配", list);
    }
}
